﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Data;
using System.Data.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;

using Lenovo.CFI.Common.Sys;
using Lenovo.CFI.Common;

namespace Lenovo.CFI.DAL.Sys
{
    public class TeamDa
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="enabled">NULL表示不限制</param>
        /// <param name="gddl">NULL表示不限制</param>
        /// <param name="manager">NULL表示不限制</param>
        /// <param name="vp">NULL表示不限制</param>
        /// <returns></returns>
        public static List<Team> GetTeam(bool? enabled, bool? gddl, string manager, string vp)
        {
            Database db = DBHelper.GetDatabase();

            DbCommand dbCommand = db.GetStoredProcCommand("Usp_GetSysTeam", enabled, gddl, manager, vp);

            List<Team> teams = new List<Team>();

            using (IDataReader dataReader = db.ExecuteReader(dbCommand))
            {
                while (dataReader.Read())
                {
                    teams.Add(PopulateTeam(dataReader));
                }
            }

            return teams;
        }

        public static Team GetTeamByID(int id)
        {
            Database db = DBHelper.GetDatabase();

            DbCommand dbCommand = db.GetStoredProcCommand("Usp_GetSysTeamByID", id);

            using (IDataReader dataReader = db.ExecuteReader(dbCommand))
            {
                if (dataReader.Read())
                {
                    return PopulateTeam(dataReader);
                }
            }

            return null;
        }

        private static Team PopulateTeam(IDataReader dataReader)
        {
            Team organ = new Team();
            organ.ID = dataReader.GetInt32(0);
            organ.Title = dataReader.GetString(1);
            organ.Enabled = dataReader.GetBoolean(2);
            organ.Manager = dataReader.IsDBNull(3) ? null : dataReader.GetString(3);
            organ.VP = dataReader.IsDBNull(4) ? null : dataReader.GetString(4);
            organ.GDDL = dataReader.GetBoolean(5);
            organ.UID = dataReader.GetGuid(6);      // 原则上不应为空

            return organ;
        }



        public static void InsertTeam(Team team)
        {
            InsertTeam(team, DBHelper.GetDatabase(), null);
        }
        public static void InsertTeam(Team team, TranscationHelper trans)
        {
            InsertTeam(team, trans.DataBase, trans.Transaction);
        }
        private static void InsertTeam(Team team, Database db, DbTransaction trans)
        {
            DbCommand dbCommand = db.GetStoredProcCommand("Usp_InsertSysTeam",
                team.Title, team.Enabled, team.Manager, team.VP, team.GDDL, team.UID);

            try
            {
                if (trans == null)
                    team.ID = Convert.ToInt32(db.ExecuteScalar(dbCommand));
                else
                    team.ID = Convert.ToInt32(db.ExecuteScalar(dbCommand, trans));
            }
            catch (System.Data.SqlClient.SqlException sex)      // 只捕获SqlException，其余抛出继续传播
            {
                DBHelper.ParseSqlException(sex, false);
            }
        }

        public static void UpdateTeam(Team team)
        {
            UpdateTeam(team, DBHelper.GetDatabase(), null);
        }
        public static void UpdateTeam(Team team, TranscationHelper trans)
        {
            UpdateTeam(team, trans.DataBase, trans.Transaction);
        }
        private static void UpdateTeam(Team team, Database db, DbTransaction trans)
        {
            DbCommand dbCommand = db.GetStoredProcCommand("Usp_UpdateSysTeam", team.ID,
                team.Title, team.Enabled, team.Manager, team.VP);

            try
            {
                if (trans == null)
                    db.ExecuteNonQuery(dbCommand);
                else
                    db.ExecuteNonQuery(dbCommand, trans);
            }
            catch (System.Data.SqlClient.SqlException sex)      // 只捕获SqlException，其余抛出继续传播
            {
                DBHelper.ParseSqlException(sex, false);
            }
        }

    }
}
